namespace GlobalData{
	//字体纹理
	export let texture:egret.Texture= new egret.Texture();
	//字体配置
	export let config:any=null;
	export let fontFamliy:any;
	/**本周最高分存储key 如score201810 2018年第10周的分数*/
	export let gameName = ""
	export let weekScoreKey:string = "";
	/**历史最高分存储key */
	export let highestScoreKey:string = "highest_score";
	/**id存储key */
	export let idKey:string = "id";
	/**用户信息 */
	export let userInfo:any = {};

	/**0-9数字图片 */
	export let textureArr:Array<egret.Texture>=[];
	/**有分数的好友关系链数据 */
	export let friendDataList:Array<UserInfo> = [];

	/**当天的字符串 */
	export let curdayNum:number = -1;
	/**更新周存储key，当当天的日期变化时重新计算一遍 */
	export function updateWeekScoreKey(){
		// 当天周几
		let curday = (new Date()).getDay();
		if(curday != GlobalData.curdayNum){
			GlobalData.curdayNum = curday;
			this.weekScoreKey = "score" + (new Date().getFullYear()) + Util.getWeekOfYear()
		}
	}
	/**
	 * 获取群组里的朋友关系链数据
	 */
	export function getGroupFriendData(userInfo:any,shareTicket:string,callback:((succss:boolean,res:any) => void) = null){
		// 加载排行榜数据
		let keyValues:Array<any> = [{fromKey:GlobalData.gameName+GlobalData.highestScoreKey,toKey:"highestScore"},
							//{fromKey:GlobalData.gameName+GlobalData.weekScoreKey,toKey:"weekScore"},
							{fromKey:GlobalData.gameName+GlobalData.idKey,toKey:"id"}];
		// 将要转换的键值取出key去微信远程获取数据
		let keyList = [];
		for(let i=0;i<keyValues.length;i++){
			let keyItem = keyValues[i];
			keyList.push(keyValues[i]["fromKey"]);
		}
        wx.getGroupCloudStorage({
            shareTicket: shareTicket,
            keyList: keyList,
            success: res => {
				let data = res["data"];
				// 清除key不一致的数据，也就是去除没有分数的数据
                let delData = Util.deleteRankData(data,keyValues,userInfo?userInfo.id:"");
				let delList = delData.list;
				let mineItem = delData.mineItem;
				if(delList.length == 0 || mineItem == null 
				|| mineItem.highestScore == undefined 
				|| mineItem.highestScore == "undefined" 
				|| parseInt(mineItem.highestScore)<parseInt(userInfo.highestScore)
				// || mineItem.weekScore == undefined 
				// || mineItem.weekScore == "undefined" 
				// || parseInt(mineItem.weekScore) != parseInt(userInfo.weekScore)
				){
					// 重新设置用户信息
                    GlobalData.saveUserInfo2GroupCloudStorage(userInfo,shareTicket,callback);
				}else{
					// 进行排序
					//delList = Util.sortScoreData(delList,"weekScore");
					delList = Util.sortScoreData(delList,"highestScore");
					if(callback){
						callback(true,delList);
					}
				}
            },
            fail: err => {
                console.log(err);
				if(callback){
					callback(false,null);
				}
            },
            complete: () => {
                
            }
        });
	}
	/**
	 * 更新用户新到微信远程服务器
	 */
    export function saveUserInfo2GroupCloudStorage(userInfo:any,shareTicket:string,callback:((succss:boolean,res:any) => void) = null) {
        if(!userInfo)return;
        let data = [
				{
					key: GlobalData.gameName+GlobalData.highestScoreKey,
					value: userInfo.highest_score + ""
				},
				{
					key: GlobalData.gameName+GlobalData.weekScoreKey,
					value: userInfo.week_highest_score + ""
				},
				{
					key: GlobalData.gameName+GlobalData.idKey,
					value: userInfo.id + ""
				}
				
			];
        wx.setUserCloudStorage({
            KVDataList: data,
            success: function (){
				GlobalData.getGroupFriendData(userInfo,shareTicket,callback);
			},
			fail: err => {
                console.log(err);
				if(callback){
					callback(false,null);
				}
            },
        });
    }
	/**
	 * 获取全部朋友关系链的数据
	 */
	 export function getAllFriendData(userInfo:any,callback:((succss:boolean,res:any) => void) = null) {
		// 加载排行榜数据
		let keyValues:Array<any> = [{fromKey:GlobalData.gameName+GlobalData.highestScoreKey,toKey:"highestScore"},
							//{fromKey:GlobalData.gameName+GlobalData.weekScoreKey,toKey:"weekScore"},
							{fromKey:GlobalData.gameName+GlobalData.idKey,toKey:"id"}];
		// 将要转换的键值取出key去微信远程获取数据
		let keyList = [];
		for(let i=0;i<keyValues.length;i++){
			let keyItem = keyValues[i];
			keyList.push(keyValues[i]["fromKey"]);
		}
        wx.getFriendCloudStorage({
            keyList: keyList,
            success: res => {
                if (!GlobalData.friendDataList) {
                    GlobalData.friendDataList = [];
                }
                let data:Array<UserInfo> = res["data"] as Array<any>;
               // 清除key不一致的数据，也就是去除没有分数的数据
                let delData = Util.deleteRankData(data,keyValues,userInfo?userInfo.id:"");
				let delList = delData.list;
				let mineItem = delData.mineItem;
				if(delList.length == 0 || mineItem == null 
				|| mineItem.highestScore == undefined 
				|| mineItem.highestScore == "undefined" 
				|| parseInt(mineItem.highestScore)<parseInt(userInfo.highestScore)){
					// 重新设置用户信息
                    GlobalData.saveUserInfo2FriendCloudStorage(userInfo,callback);
				}else{
					// 进行排序
					//delList = Util.sortScoreData(delList,"weekScore");
					delList = Util.sortScoreData(delList,"highestScore");
					// 缓存全部朋友分数数据
                	GlobalData.friendDataList = delList;
					if(callback){
						callback(true,delList);
					}
				}
            },
            fail: err => {
                console.log(err);
				if(callback){
					callback(false,null);
				}
            },
            complete: () => {
            }
        });
    }
	/**
	 * 更新用户新到微信远程服务器
	 */
    export function saveUserInfo2FriendCloudStorage(userInfo:any,callback:((succss:boolean,res:any) => void) = null) {
        if(!userInfo)return;
        let data = [
				{
					key: GlobalData.gameName+GlobalData.highestScoreKey,
					value: userInfo.highest_score + ""
				},
				{
					key: GlobalData.gameName+GlobalData.weekScoreKey,
					value: userInfo.week_highest_score + ""
				},
				{
					key: GlobalData.gameName+GlobalData.idKey,
					value: userInfo.id + ""
				}
				
			];
        wx.setUserCloudStorage({
            KVDataList: data,
            success: function (){
				GlobalData.getAllFriendData(userInfo,callback);
			},
			fail: err => {
                console.log(err);
				if(callback){
					callback(false,null);
				}
            },
        });
    }
	
}